﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentValidation;
using Microhard.Core;
using FluentValidation.Results;
using Microhard.Plat.CompanyChargingMng.Models;
using Microhard.Plat.CompanyChargingMng.Msg;
using Microhard.Plat.CompanyChargingMng.Interfaces;

namespace Microhard.Plat.CompanyChargingMng.Validators
{
    public class EmplyeeModelValidator : AbstractValidator<EmployeeModel>
    {
        public EmplyeeModelValidator()
        {
            RuleSet("Insert", () =>
            {
                RuleFor(u => u.Name).NotEmpty().WithMessage(EmployeeMsg.NeedName).When(r => r.EmployeeID == Guid.Empty);
                RuleFor(u => u.Name).Length(1, 50).WithMessage(EmployeeMsg.MaxNameLength).When(r => r.EmployeeID == Guid.Empty);

                RuleFor(u => u.UserName).NotEmpty().WithMessage(EmployeeMsg.NeedUserName).When(r => r.EmployeeID == Guid.Empty);

                RuleFor(u => u.QQMail).NotEmpty().WithMessage(EmployeeMsg.NeedQQMail).When(r => r.EmployeeID == Guid.Empty);
                RuleFor(u => u.QQMail).Length(1, 50).WithMessage(EmployeeMsg.QQMailMaxLength).When(r => r.EmployeeID == Guid.Empty);

                RuleFor(u => u.Cellphone).NotEmpty().WithMessage(EmployeeMsg.NeedCellphone).When(r => r.EmployeeID == Guid.Empty);
                RuleFor(u => u.Cellphone).Length(1, 50).WithMessage(EmployeeMsg.CellphoneMaxLength).When(r => r.EmployeeID == Guid.Empty);

                RuleFor(u => u.BelongCompanyName).NotEmpty().WithMessage(EmployeeMsg.NeedCmp).When(r => r.EmployeeID == Guid.Empty);

                Custom(u =>
                {
                    IEmployee emp = IoC.Resolve<IEmployee>();
                    if (emp.SystemAccountIsUsing(u.UserName))
                    {
                        return new ValidationFailure("UserName", EmployeeMsg.UserAccountIsUsing);
                    }
                    return null;
                });
            });

            RuleSet("Update", () =>
            {
                RuleFor(u => u.Name).NotEmpty().WithMessage(EmployeeMsg.NeedName).When(r => r.EmployeeID == Guid.Empty);
                RuleFor(u => u.Name).Length(1, 50).WithMessage(EmployeeMsg.MaxNameLength).When(r => r.EmployeeID == Guid.Empty);

                RuleFor(u => u.QQMail).NotEmpty().WithMessage(EmployeeMsg.NeedQQMail).When(r => r.EmployeeID == Guid.Empty);
                RuleFor(u => u.QQMail).Length(1, 50).WithMessage(EmployeeMsg.QQMailMaxLength).When(r => r.EmployeeID == Guid.Empty);

                RuleFor(u => u.Cellphone).NotEmpty().WithMessage(EmployeeMsg.NeedCellphone).When(r => r.EmployeeID == Guid.Empty);
                RuleFor(u => u.Cellphone).Length(1, 50).WithMessage(EmployeeMsg.CellphoneMaxLength).When(r => r.EmployeeID == Guid.Empty);
            });
        }
    }
}
